<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">综测管理系统</h2>
      
      <!-- 身份选择 -->
      <el-radio-group v-model="identity" class="identity-group">
        <el-radio-button label="organizer">组织方</el-radio-button>
        <el-radio-button label="student">学生方</el-radio-button>
      </el-radio-group>

      <!-- 登录表单 -->
      <el-form 
        ref="loginFormRef" 
        :model="loginForm" 
        :rules="loginRules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入账号/学号"
            :prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <el-button 
          type="primary" 
          class="login-button" 
          @click="handleLogin"
        >
          登录
        </el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'

const router = useRouter()
const identity = ref('organizer') // 默认组织方
const loginFormRef = ref<FormInstance>()

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = reactive<FormRules>({
  username: [{ required: true, message: '请输入账号/学号', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
})

// 登录处理
const handleLogin = async () => {
  // 表单验证
  const valid = await loginFormRef.value?.validate()
  if (!valid) return
  
  try {
    // 实际项目中替换为真实API调用
    // const res = await loginAPI({ 
    //   username: loginForm.username,
    //   password: loginForm.password,
    //   identity: identity.value
    // })
    
    // 模拟登录成功响应
    const token = `token-${Date.now()}`
    
    // 存储token和身份
    localStorage.setItem('token', token)
    localStorage.setItem('identity', identity.value)
    
    ElMessage.success('登录成功')
    
    // 根据身份跳转
    if (identity.value === 'organizer') {
      router.push('/upload')
    } else {
      router.push('/student/score')
    }
    
  } catch (error) {
    ElMessage.error('登录失败，请检查账号密码')
    console.error('登录错误:', error)
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.login-card {
  width: 400px;
  padding: 30px;
  border-radius: 8px;
}

.login-title {
  text-align: center;
  margin-bottom: 24px;
  color: #409EFF;
}

.identity-group {
  display: flex;
  justify-content: center;
  margin-bottom: 24px;
}

.login-button {
  width: 100%;
  margin-top: 10px;
}
</style>